#!/bin/bash
# useage: sudo ./switch_mode.sh [monitor|managed]
# 切换Wi-Fi接口在监听模式和正常模式之间

# 默认接口
INTERFACE="wlan0"
CHANNEL=6  # 设置为通道6（2.437 GHz）用于监听模式

# 检查是否具有根权限
if [[ $EUID -ne 0 ]]; then
    echo "Error: This script must be run as root. Use 'sudo $0 <mode>'"
    exit 1
fi

# Check if airmon-ng is installed
if ! command -v airmon-ng &> /dev/null; then
    echo "Error: airmon-ng not found. Install aircrack-ng: 'sudo apt install aircrack-ng'"
    exit 1
fi

# Check if iwconfig is installed
if ! command -v iwconfig &> /dev/null; then
    echo "Error: iwconfig not found. Install wireless-tools: 'sudo apt install wireless-tools'"
    exit 1
fi

# Usage function
usage() {
    echo "Usage: $0 <mode>"
    echo "Modes:"
    echo "  monitor  - Switch to monitor mode (wlan0mon)"
    echo "  managed  - Switch to managed mode (wlan0)"
    exit 1
}

# Check if mode is provided
if [ -z "$1" ]; then
    usage
fi

MODE="$1"

# Function to switch to monitor mode
switch_to_monitor() {
    echo "Switching $INTERFACE to monitor mode..."

    # Stop NetworkManager to avoid interference
    systemctl stop NetworkManager 2>/dev/null || echo "Warning: Failed to stop NetworkManager"

    # Check for processes that might interfere
    airmon-ng check kill

    # Bring interface down
    ip link set $INTERFACE down || { echo "Error: Failed to bring $INTERFACE down"; exit 1; }

    # Start monitor mode
    airmon-ng start $INTERFACE >/dev/null || { echo "Error: Failed to start monitor mode"; exit 1; }

    # Verify monitor mode
    if iwconfig wlan0mon 2>/dev/null | grep -q "Mode:Monitor"; then
        echo "Monitor mode enabled on wlan0mon"
    else
        echo "Error: Failed to enable monitor mode"
        airmon-ng stop wlan0mon >/dev/null
        ip link set $INTERFACE up
        exit 1
    fi

    # Set channel to 6 (2.437 GHz)
    iwconfig wlan0mon channel $CHANNEL || { echo "Error: Failed to set channel $CHANNEL"; exit 1; }
    echo "Set wlan0mon to channel $CHANNEL (2.437 GHz)"

    # Bring interface up
    ip link set wlan0mon up || { echo "Error: Failed to bring wlan0mon up"; exit 1; }
    echo "Interface wlan0mon is up and ready"
}

# Function to switch to managed mode
switch_to_managed() {
    echo "Switching $INTERFACE to managed mode..."

    # Stop monitor mode if active
    if ip link show wlan0mon >/dev/null 2>&1; then
        airmon-ng stop wlan0mon >/dev/null || { echo "Error: Failed to stop monitor mode"; exit 1; }
        echo "Monitor mode stopped"
    fi

    # Bring interface down
    ip link set $INTERFACE down || { echo "Error: Failed to bring $INTERFACE down"; exit 1; }

    # Set to managed mode
    iwconfig $INTERFACE mode managed || { echo "Error: Failed to set managed mode"; exit 1; }

    # Bring interface up
    ip link set $INTERFACE up || { echo "Error: Failed to bring $INTERFACE up"; exit 1; }

    # Restart NetworkManager
    systemctl start NetworkManager 2>/dev/null || echo "Warning: Failed to start NetworkManager"

    # Verify managed mode
    if iwconfig $INTERFACE 2>/dev/null | grep -q "Mode:Managed"; then
        echo "Managed mode enabled on $INTERFACE"
    else
        echo "Error: Failed to enable managed mode"
        exit 1
    fi
}

# Main logic
case "$MODE" in
    monitor)
        switch_to_monitor
        ;;
    managed)
        switch_to_managed
        ;;
    *)
        usage
        ;;
esac

# Display current interface status
echo "Current interface status:"
iwconfig $INTERFACE 2>/dev/null || iwconfig wlan0mon 2>/dev/null
